#include"common_headers.h"
//--------------------------create----------------------------------------------
int validateFileExistance(char name[50])
{
	if(access(name, F_OK)!=0)
		return 0;	//FILE DOES NOT EXIST
	else
		return 1;	//FILE EXIST
}

int validateFileName(char name[50])
{
	int index, length_of_filename;
	
	length_of_filename=strlen(name);
	for(index=0; index<length_of_filename; index++)
	{
		if(name[index]=='/')
			return 0; 	//INVALID FILENAME
	}
	return 1;			//VALID FILENAME
}

int validateSize(int size)
{
	if((size>=1 && size<=1024))
		return 1;		//VALID SIZE
	else
		return 0;		//INVALID SIZE
}


//--------------------------------mount-----------------------------------------

int validateMountState()
{
	if(mountstate==0)
		return 0;		//NOT MOUNTED
	else
		return 1;		//MOUNTED
}

int validateMountingFileExistance(char name[50])
{
	if(access(name, F_OK)!=0)
		return 0;	//FILE DOES NOT EXIST
	else
		return 1;	//FILE ALREADY EXIST
}

int validateMountingFileName(char name[50])
{
	int index, length_of_filename;
	
	length_of_filename=strlen(name);
	for(index=0; index<length_of_filename; index++)
	{
		if(name[index]=='/')
			return 0; 	//INVALID FILENAME
	}
	return 1;			//VALID FILENAME
}


//--------------------------------unmount---------------------------------------



int validateUnMountingFileName(char name[50])
{
	int index, length_of_filename;
	
	length_of_filename=strlen(name);
	for(index=0; index<length_of_filename; index++)
	{
		if(name[index]=='/')
			return 0; 	//INVALID FILENAME
	}
	return 1;			//VALID FILENAME
}

int validateUnMountingFileExistance(char name[50])
{
	if(access(name, F_OK)!=0)
		return 0;	//FILE DOES NOT EXIST
	else
		return 1;	//FILE ALREADY EXIST
}

int validateMountStateBeforeUnmounting()
{
	if(mountstate==0)
		return 0;		//NOT MOUNTED
	else
		return 1;		//MOUNTED
}


//--------------------------general---------------------------------------------

//VALIDATING FILE EXTENSION TO BE .txt (IN THIS CASE)
int validateTextFile(char name[50])
{
	//DECLARATIONS
	int length_of_filename, comparison;
	char last_character, last_butOne_character, last_butTwo_character, last_butThree_character;
	char text_file_extension[]="****", new_string[] = ".txt";
		
	//INITIALIZATION
	length_of_filename=0;
	comparison = 1;
	
	length_of_filename = strlen(name);

	//EXTRACTING LAST 4 CHARACTERS (EXTENSION) FROM FILENAME RECEIVED (CHARACTER BY CHARACTER) 
	last_character = name[length_of_filename - 1];
	last_butOne_character = name[length_of_filename - 2];
	last_butTwo_character = name[length_of_filename - 3];
	last_butThree_character = name[length_of_filename - 4];
	
	//ASSIGNING THE EXTRACTED CHARACTERS TO A TEMPORARY STRING VARIABLE (CHARACTER BY CHARACTER)
	text_file_extension[0] = last_butThree_character;
	text_file_extension[1] = last_butTwo_character;
	text_file_extension[2] = last_butOne_character;
	text_file_extension[3] = last_character;
	
	//COMAPRING THE EXTENSION OF RECEIVED FILE WITH THE ALLOWED EXTENSION (.txt) IN THIS CASE
	comparison = strcmp(new_string, text_file_extension);	
	
	if(comparison==0)
	{
		return 1;	//SUCCESS (FILE IS A .txt FILE)
	}
	else
	{
		return 0;	//FAILURE (FILE IS NOT A .txt FILE)
	}
}





//----------------------------------------------------

int validateDirectoryName(char name[50])
{
	int index, length_of_filename;
	
	length_of_filename=strlen(name);
	for(index=0; index<length_of_filename; index++)
	{
		if(name[index]=='.')
			return 0; 	//INVALID DIRNAME
	}
	return 1;			//VALID DIRNAME
}
